package builder 
{
	import app.textures.TextureInfo;
	import fl.controls.Button;
	import flash.display.Loader;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.IOErrorEvent;
	import flash.events.MouseEvent;
	import flash.events.SecurityErrorEvent;
	import flash.net.FileReference;
	import flash.net.URLRequest;
	import flash.text.TextField;
	/**
	 * ...
	 * @author Dmitriy Gaiduk
	 */
	public class MakeXML extends Sprite
	{
		private var _fileRef:FileReference;
		private var _textures:Vector.<TextureInfo>;
		private var xmlData:XML;
		private var _totalItems:int;
		private var _loadedItems:int;
		private var loaders:Vector.<LoaderColorFromImage>;
		private var _text:TextField;
		private var _btn:Button;
		
		public function MakeXML() 
		{
			initMe();
		}
		
		private function initMe():void 
		{
			_btn = new Button();
			addChild(_btn);
			_btn.label = "Save";
			_btn.x = 320;
			_btn.y = 20;
			_btn.addEventListener(MouseEvent.CLICK, handleBtnClick);
			_text = new TextField();
			_text.width = 300;
			_text.height = 200;
			addChild(_text);
			_fileRef = new FileReference();
			_fileRef.addEventListener(IOErrorEvent.IO_ERROR, hndlFileRefIOError);
			_fileRef.addEventListener(SecurityErrorEvent.SECURITY_ERROR, hndlFileRefSecurityErr);
			
			// texture
			_textures = new Vector.<TextureInfo>();
			//createRAL();
			//createTexture("stone/preview/","stone/texture/", 1,48);
			createTexture("plastic/preview/","plastic/texture/", 1,32);
			
		}
		
		private function createTexture(folderPrew:String, folderTexture:String, from:int, to:int):void
		{
			loaders = new Vector.<LoaderColorFromImage>();
			var fileName:String;
			for (var i:int = from; i <= to; i++ )
			{
				fileName = i.toString();
				if (fileName.length == 1)
				{
					fileName = "0" + fileName;
				}
				fileName = fileName + ".jpg";
				var textureInfo:TextureInfo = new TextureInfo(null);
				_textures.push(textureInfo);
				textureInfo.previewURL = folderPrew + fileName;
				textureInfo.sourceURL = folderTexture + fileName;
				textureInfo.name = fileName;
				var loader:LoaderColorFromImage = new LoaderColorFromImage(textureInfo, true);
				loaders.push(loader);
			}
			
			runLoaders();
		}
		
		private function handleBtnClick(e:MouseEvent):void 
		{
			_fileRef.save(xmlData);
		}
		
		private function createRAL():void 
		{
			_loadedItems = 0;
			loaders = new Vector.<LoaderColorFromImage>();
			var folder:String = "ral/";
			addRAL(folder, 1000, 1500);
			//addRAL(folder, 1000, 9018);
			//addRAL(folder, 1000, 1000);
			
			runLoaders();
			//_fileRef.save(texturesXML);
		}
		
		private function runLoaders():void 
		{
			for each (var loader:LoaderColorFromImage in loaders)
			{
				loader.addEventListener(Event.CHANGE, handleLoaderChange);
				loader.start();
			}
		}
		
		private function hndlFileRefSecurityErr(e:SecurityErrorEvent):void 
		{
			trace("File ref secutity err");
		}
		
		private function hndlFileRefIOError(e:IOErrorEvent):void 
		{
			trace("File ref IO err");
		}
		
		
		private function addRAL(folder:String, from:int, to:int):void
		{
			
			for (var i:int = from; i <= to; i++ )
			{
				var textureInfo:TextureInfo = new TextureInfo(null);
				textureInfo.previewURL = folder + "RAL-" + i.toString() + ".jpg";
				textureInfo.name = "RAL " + i.toString();
				var loader:LoaderColorFromImage = new LoaderColorFromImage(textureInfo, true);
				_textures.push(textureInfo);
				loaders.push(loader);
			}
		}
		
		private function handleLoaderChange(e:Event):void 
		{
			_loadedItems++;
			if (_loadedItems == loaders.length)
			{
				makeRALXml();
			}
		}
		
		private function makeRALXml():void
		{
			trace("Ready!");
			xmlData = <data></data>;
			for each(var textureInf:TextureInfo in _textures)
			{
				if (textureInf.previewURL != "empty")
				{
					var textureXML:XML = <texture />
					textureXML.@preview = textureInf.previewURL;
					if ((textureInf.sourceURL != null) && (textureInf.sourceURL.length > 0))
					{
						textureXML.@texture = textureInf.sourceURL;
					}else
					{
						textureXML.@color = textureInf.color;
					}
					textureXML.@title = textureInf.name;
					xmlData = xmlData.appendChild(textureXML);
				}
			}
			_text.text = xmlData;
		}
		
	}

}